﻿using System;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;

namespace Web.Pages.YongShui._13.LAIHANGL
{
    public partial class JiHuaZYSL : BasePage
    {
        #region 属性

        #endregion

        #region 变量
        /// <summary>
        /// 访问来函表
        /// </summary>
        YS_LH_LAIHANXXProcess blllh = new YS_LH_LAIHANXXProcess();
        /// <summary>
        /// 来函用户信息
        /// </summary>
        YS_LH_YONGHUXXProcess laihanyhxx = new YS_LH_YONGHUXXProcess();

        /// <summary>
        /// 访问数据库
        /// </summary>
        SpHelper _sphelper = new SpHelper();
        /// <summary>
        /// 来函计划转移表
        /// </summary>
        YS_LH_JIHUAZYProcess bllzyjh = new YS_LH_JIHUAZYProcess();

        /// <summary>
        /// 流程表
        /// </summary>
        YS_LH_LAIHANLCJLBLL lcbll = new YS_LH_LAIHANLCJLBLL();
        #endregion

        #region 事件
        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                
            }
        }
        /// <summary>
        /// 退回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnBack_Click(object sender, EventArgs e)
        {
            Save(-2);
        }
        /// <summary>
        /// 受理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSL_Click(object sender, EventArgs e)
        {
            Save(2);
        }
        /// <summary>
        /// 不受理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnBSL_Click(object sender, EventArgs e)
        {
            Save(-1);
        }
        #endregion

        #region 方法
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="type">-2退回 2受理 -1不受理</param>
        private void Save(int type)
        {

            if (txtLiYou.Text.Trim().ToString().Length == 0)
            {

                PageTools.AlertMsg(this, "申请理由不能为空");
                return;
            }

            string updatecol = "";
            updatecol = " I_liuChengBH=0 ";
            updatecol = string.Format(updatecol, txtLiYou.Value, txtBeiZhu.Value, LoginInfo.CurrentUser.UserID);
            string wherecase = string.Format(" ID={0}", h_laihanjhid.Value);

            string laihanzbupdatecol = string.Format("I_LIUCHENGBH={0},S_SHENQINGLY='{1}',S_BEIZHU='{2}'", 3, txtLiYou.Text, txtBeiZhu.Text);
            string where = string.Format(" ID={0} ", laihanid.Value);
            if (type == -1)
            {
                laihanzbupdatecol = string.Format("S_SHENQINGLY='{1}',S_BEIZHU='{2}' ,S_LIUCHENGZT=", type, txtLiYou.Text, txtBeiZhu.Text);

            }
            else if (type == -2)
            {
                bllzyjh.UpdateYS_LH_JIHUAZiesDynamic(updatecol,wherecase);
                laihanzbupdatecol = string.Format("I_LIUCHENGBH={0},S_SHENQINGLY='{1}',S_BEIZHU='{2}'", 0, txtLiYou.Text, txtBeiZhu.Text);

            }
            int success = blllh.UpdateYS_LH_LAIHANXXesDynamic(laihanzbupdatecol, where);
            if (success > 0)
            {
                ///添加受理开始信息
                YS_LH_LAIHANLCJL lcjl = new YS_LH_LAIHANLCJL();
                lcjl.D_CAOZUORQ = DateTime.Now;
                lcjl.D_STARTDATE = DateTime.Now;
                lcjl.I_CAOZUOR = LoginInfo.CurrentUser.UserID;
                lcjl.I_LAIHANLX = 1;
                lcjl.I_LIUCHENGBH = type;
                lcjl.ID = 0;
                if (type == -2 || type == -1)
                {
                    lcjl.D_ENDDATE = DateTime.Now;
                }

                DataTable lcxx = lcbll.SelectYS_LH_LAIHANLCJLsDynamic(string.Format(" S_LAIHANBH='{0}' and I_LIUCHENGBH={1} ", h_laihan.Value, type), "", null);
                if (lcxx != null && lcxx.Rows.Count > 0)
                {
                    lcjl.ID = Convert.ToInt32(lcxx.Rows[0]["ID"]);
                }
                lcjl.S_LAIHANBH = h_laihan.Value;

                lcjl.S_LIUCHENGMC = type == 2 ? "受理" : type == -1 ? "不受理" : "退回";

                lcbll.InsertOrUpdate(lcjl);

                ///更新受理结束事件  受理事件开始结束同时
                lcbll.UpdateYS_LH_LAIHANLCJLsDynamic(string.Format("D_ENDDATE=sysdate,I_CAOZUOR={0},D_CAOZUORQ=sysdate", LoginInfo.CurrentUser.UserID), " I_LIUCHENGBH=2 and S_LAIHANBH='" + lcjl.S_LAIHANBH + "'");
                ///受理结束直接进入下个流程
                if (type > 0)
                {
                    AddYEWUYUAN();
                    lcbll.UpdateYS_LH_LAIHANLCJLsDynamic(string.Format("D_ENDDATE=sysdate,I_CAOZUOR={0},D_CAOZUORQ=sysdate", LoginInfo.CurrentUser.UserID), " I_LIUCHENGBH=1 and S_LAIHANBH='" + lcjl.S_LAIHANBH + "'");
                
                }
                PageTools.AlertMsg(this, "操作完成");

                System.Web.UI.ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), DateTime.Now.ToString("yyyyMMddHHmmssfff"), new System.Text.StringBuilder("window.close();").ToString(), true);

            }

        }
        /// <summary>
        /// 添加业务员开始信息
        /// </summary>
        private void AddYEWUYUAN()
        {
            ///添加流程记录业务员意见开始信息
            YS_LH_LAIHANLCJL lcjl = new YS_LH_LAIHANLCJL();
            lcjl.D_CAOZUORQ = DateTime.Now;
            lcjl.D_STARTDATE = DateTime.Now;
            lcjl.I_CAOZUOR = LoginInfo.CurrentUser.UserID;
            lcjl.I_LAIHANLX = 1;
            lcjl.I_LIUCHENGBH = 3;
            lcjl.ID = 0;

            DataTable lcxx = lcbll.SelectYS_LH_LAIHANLCJLsDynamic(string.Format(" S_LAIHANBH='{0}' and I_LIUCHENGBH={1} ", h_laihan, 3), "", null);
            if (lcxx != null && lcxx.Rows.Count > 0)
            {
                lcjl.ID = Convert.ToInt32(lcxx.Rows[0]["ID"]);
            }
            lcjl.S_LAIHANBH =h_laihan.Value;

            lcjl.S_LIUCHENGMC = "业务员意见";

            lcbll.InsertOrUpdate(lcjl);

        }
        /// <summary>
        /// 绑定原始信息
        /// </summary>
        private void BindData()
        {
            if (string.IsNullOrEmpty(Request["id"]) || Request["id"] == "null")
            {
                return;
            }
            int id = Convert.ToInt32(Request["id"]);
            YS_LH_LAIHANXX laihan = blllh.SelectYS_LH_LAIHANXX(id);
            if (laihan != null)
            {
                laihanid.Value = Request["id"];
                h_laihan.Value = laihan.S_LAIHANBH;
                h_yonghu.Value = laihan.S_YONGHUBH;
                HLaiHanLX.Set("laihanlx", laihan.I_LAIHANLX);
                string Jyonghu = laihan.S_YONGHUBH;
                string Yyonghu = "";
                string where = " S_LAIHANBH='{0}' and S_YONGHUBHJ='{1}' and I_LIUCHENGBH=1 ";
                where = string.Format(where, laihan.S_LAIHANBH, laihan.S_YONGHUBH);
                DataTable JiHuaZY = bllzyjh.SelectYS_LH_JIHUAZiesDynamic(where, "", null);
                if (JiHuaZY != null && JiHuaZY.Rows.Count > 0)
                {
                    h_laihanjhid.Value = JiHuaZY.Rows[0]["ID"].ToString();
                    Yyonghu = JiHuaZY.Rows[0]["S_YONGHUBHY"].ToString();
                }
                else
                {
                    PageTools.AlertMsg(this, "相关来函信息不存在");
                    return;
                }

                UCLHJiHuaZYUserInfo.LoadInfo(Jyonghu, Yyonghu, laihan);                

                UCLHJiHuanZYJHXX.BindJHXX(UCLHJiHuaZYUserInfo.BindJHXX(1), 1);
                UCLHJiHuanZYJHXX.BindJHXX(UCLHJiHuaZYUserInfo.BindJHXX(2), 2);
                UCLHJiHuanZYJHXX.BindJHL(JiHuaZY);
                UCLHJiHuanZYJHXX.SetShouLiStatus();
                UCLHJiHuaZYUserInfo.SetShouLiStatus();


            }
            else
            {
                PageTools.AlertMsg(this, "相关来函信息不存在");
                return;
            }
           

        }
        /// <summary>
        /// 页面孔家加载完全后绑定值
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoadComplete(EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        #endregion

    }
}